﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiMovAviGeneratorForm
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands;

public class CUiMovAviGeneratorForm : TxForm
{
  private Button m_CancelButton;
  private Label m_createdMoviesLabel;
  private ProgressBar m_generatedFilesProgressBar;
  private IContainer components;
  private CApMovAviGeneratorApp m_aviGeneratorApp;
  private int m_selectedObjectsCount;
  private int m_createdMoviesCount;
  private string m_updatedCountMessage;
  private Point m_CenterOfParent;
  private CApMovMovieManagerApp.AVIGenerationMode m_generationMode;
  private ArrayList m_selectedObjects;

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiMovAviGeneratorForm));
    this.m_generatedFilesProgressBar = new ProgressBar();
    this.m_createdMoviesLabel = new Label();
    this.m_CancelButton = new Button();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.m_generatedFilesProgressBar, "m_generatedFilesProgressBar");
    this.m_generatedFilesProgressBar.Name = "m_generatedFilesProgressBar";
    componentResourceManager.ApplyResources((object) this.m_createdMoviesLabel, "m_createdMoviesLabel");
    this.m_createdMoviesLabel.Name = "m_createdMoviesLabel";
    componentResourceManager.ApplyResources((object) this.m_CancelButton, "m_CancelButton");
    this.m_CancelButton.Cursor = Cursors.Arrow;
    this.m_CancelButton.DialogResult = DialogResult.Cancel;
    this.m_CancelButton.Name = "m_CancelButton";
    this.m_CancelButton.Click += new EventHandler(this.m_CancelButton_Click);
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((ContainerControl) this).AutoScaleDimensions = new SizeF(6f, 13f);
    ((Form) this).CancelButton = (IButtonControl) this.m_CancelButton;
    ((Form) this).ControlBox = false;
    ((Control) this).Controls.Add((Control) this.m_CancelButton);
    ((Control) this).Controls.Add((Control) this.m_createdMoviesLabel);
    ((Control) this).Controls.Add((Control) this.m_generatedFilesProgressBar);
    ((Form) this).FormBorderStyle = FormBorderStyle.FixedToolWindow;
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiMovAviGeneratorForm);
    ((Form) this).Shown += new EventHandler(this.CUiMovAviGeneratorForm_Shown);
    ((Form) this).Closing += new CancelEventHandler(this.CUiMovAviGeneratorForm_Closing);
    ((Form) this).Closed += new EventHandler(this.CUiMovAviGeneratorForm_Closed);
    ((Control) this).ResumeLayout(false);
  }

  public CUiMovAviGeneratorForm(Rectangle bounds, CApMovMovieRecorder recorder)
  {
    this.InitializeComponent();
    this.m_updatedCountMessage = "";
    this.m_aviGeneratorApp = new CApMovAviGeneratorApp(recorder);
    this.m_CenterOfParent = new Point((bounds.Width - ((Control) this).Width) / 2 + bounds.X, (bounds.Height - ((Control) this).Height) / 2 + bounds.Y);
  }

  public DialogResult ShowDialog(
    TxForm parent,
    CApMovMovieManagerApp.AVIGenerationMode generationMode,
    ArrayList allObjects)
  {
    DialogResult dialogResult = DialogResult.OK;
    this.m_generationMode = generationMode;
    this.m_selectedObjects = allObjects;
    this.m_selectedObjectsCount = this.m_aviGeneratorApp.GetSelectedObjectCountToGenerateMovies(this.m_generationMode, this.m_selectedObjects);
    if (this.m_selectedObjectsCount > 0)
      dialogResult = ((Form) this).ShowDialog((IWin32Window) parent);
    else if (generationMode == CApMovMovieManagerApp.AVIGenerationMode.CreateMissingMovies)
      this.ShowMoviesAlreadyExistMessage();
    return dialogResult;
  }

  private void CUiMovAviGeneratorForm_Shown(object sender, EventArgs e)
  {
    ITxOperation currentOperation = TxApplication.ActiveDocument.CurrentOperation;
    try
    {
      if (this.m_selectedObjectsCount <= 0)
        return;
      Point centerOfParent = this.m_CenterOfParent;
      ((Form) this).Location = this.m_CenterOfParent;
      this.RegisterToAVICreationEvent();
      this.RegisterToSimulationStopedEvent();
      this.InitializeProgressBar();
      this.InitializeProgressBarText();
      this.UpdateDialogWithProgress(false);
      this.m_aviGeneratorApp.StartGenerationProcess(this.m_generationMode, this.m_selectedObjects);
    }
    catch
    {
    }
    finally
    {
      TxApplication.ActiveDocument.CurrentOperation = currentOperation;
      ((Form) this).Close();
    }
  }

  public void CUiMovAviGeneratorForm_Closed(object sender, EventArgs e)
  {
    ((Control) this).Hide();
    if (((Form) this).Owner != null)
      ((Form) this).Owner.Activate();
    if (this.m_selectedObjectsCount == 0)
      return;
    this.m_aviGeneratorApp.ShowDetailedMessage(this.m_selectedObjectsCount - this.m_createdMoviesCount);
  }

  private void RegisterToAVICreationEvent()
  {
    this.m_aviGeneratorApp.AVIAttachmentCreated += new CApMovAviGeneratorApp.CApMovAviGenerator_AVIAttachmentCreatedEventHandler(this.m_aviGeneratorApp_AVIAttachmentCreated);
  }

  private void UnRegisterFromAVICreationEvent()
  {
    this.m_aviGeneratorApp.AVIAttachmentCreated -= new CApMovAviGeneratorApp.CApMovAviGenerator_AVIAttachmentCreatedEventHandler(this.m_aviGeneratorApp_AVIAttachmentCreated);
  }

  private void RegisterToSimulationStopedEvent()
  {
    this.m_aviGeneratorApp.SimulationStopedOnUserRequest += new CApMovAviGeneratorApp.CApMovAviGenerator_SimulationStopedOnUserRequestEventHandler(this.m_aviGeneratorApp_SimulationStoped);
  }

  private void UnRegisterSimulationStopedEvent()
  {
    this.m_aviGeneratorApp.SimulationStopedOnUserRequest -= new CApMovAviGeneratorApp.CApMovAviGenerator_SimulationStopedOnUserRequestEventHandler(this.m_aviGeneratorApp_SimulationStoped);
  }

  private void InitializeProgressBar()
  {
    this.m_generatedFilesProgressBar.Value = 0;
    this.m_generatedFilesProgressBar.Minimum = 0;
    this.m_generatedFilesProgressBar.Maximum = this.m_selectedObjectsCount * 10;
    this.m_generatedFilesProgressBar.Step = 10;
  }

  private void InitializeProgressBarText()
  {
    this.m_updatedCountMessage = new ResourceManager("DnProcessSimulateCommands.StringTable", ((object) this).GetType().Assembly).GetString("MOVIE_MANAGER_PROGRESSBAR_PROGRESS_MESSAGE");
  }

  private void UpdateDialogWithProgress(bool shouldUpdateProgressBar)
  {
    this.m_createdMoviesLabel.Text = string.Format(this.m_updatedCountMessage, (object) this.m_createdMoviesCount.ToString(), (object) this.m_selectedObjectsCount.ToString());
    if (shouldUpdateProgressBar)
      this.m_generatedFilesProgressBar.Value += this.m_generatedFilesProgressBar.Step;
    ((Control) this).Refresh();
    Application.DoEvents();
  }

  private void m_aviGeneratorApp_AVIAttachmentCreated(
    object sender,
    CApMovAviGeneratorApp.CApMovAviGenerator_AVICreatedEventArgs args)
  {
    this.m_createdMoviesCount = this.m_aviGeneratorApp.CreatedMoviesCount;
    this.UpdateDialogWithProgress(true);
  }

  private void m_aviGeneratorApp_SimulationStoped(object sender, EventArgs args)
  {
    ((Form) this).Close();
  }

  private void m_CancelButton_Click(object sender, EventArgs e)
  {
    this.m_aviGeneratorApp.StopGenerationProcess();
    Cursor.Current = Cursors.WaitCursor;
    ((Control) this).Cursor = Cursors.WaitCursor;
    this.m_CancelButton.Enabled = false;
    Application.DoEvents();
  }

  private void CUiMovAviGeneratorForm_Closing(object sender, CancelEventArgs e)
  {
    this.UnRegisterFromAVICreationEvent();
    this.UnRegisterSimulationStopedEvent();
    this.m_aviGeneratorApp.CleanTemporaryChanges();
  }

  private void ShowMoviesAlreadyExistMessage()
  {
    ResourceManager resourceManager = new ResourceManager("DnProcessSimulateCommands.StringTable", ((object) this).GetType().Assembly);
    int num = (int) TxMessageBox.Show(resourceManager.GetString("MOVIE_MANAGER_NO_MISSING_MOVIES"), resourceManager.GetString("MOVIE_MANAGER_COMMAND_NAME"), MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  }

  public delegate void CApMovAviGenerator_AVIAttachmentCreatedEventHandler(
    object sender,
    CApMovAviGeneratorApp.CApMovAviGenerator_AVICreatedEventArgs args);

  public delegate void CApMovAviGenerator_SimulationStopedOnUserRequestEventHandler(
    object sender,
    EventArgs args);
}
